package org.jz.gae.redbook.spb.client.toolkit;

import java.util.ArrayList;
import java.util.List;

import org.jz.gae.redbook.spb.client.RedBookServiceAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Label;

public class ApplicationContainer extends Composite
{
	private FlexTable rootPanel = new FlexTable();
	private SimplePanel slateContainerPanel = new SimplePanel();
	private Button slateCloseButton = new Button("x");
	private Label slateCaptionLabel = new Label(); 
	private SlateListPanel slateListPanel = new SlateListPanel();
	
	private List<Slate> slateList = new ArrayList<Slate>();
	
	private ApplicationAgent applicationAgent = new ApplicationAgentImpl();
	private HandlerManager eventBus;
	private RedBookServiceAsync rpcService;
	private Slate activeSlate;
	
	private static final String SLATE_LIST_PANEL_WIDTH = "140px";
	
	public ApplicationContainer() 
	{
		SimplePanel contentTableDecorator = new SimplePanel();
	    initWidget(contentTableDecorator);
	    contentTableDecorator.addStyleName("applicationContainer");
	    contentTableDecorator.setWidth("100%");
		initializeInterface();
	    contentTableDecorator.add(rootPanel);
	    /**/
	    slateListPanel.setSlateList(slateList);
	    slateListPanel.setApplicationAgent(applicationAgent);
	    slateCloseButton.addClickHandler(new  ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				performSlateClose();
			}
		});
	}

	private void initializeInterface()
	{
		rootPanel.addStyleName("applicationRootPanel");
		rootPanel.setWidth("100%");
		    SimplePanel slateListCaptionPanel = new SimplePanel();
		    slateListCaptionPanel.addStyleName("applicationSlateListCaption");
			//captionPanel.setWidth("100%");
		        Label slateListCaptionLabel = new Label();
		        slateListCaptionLabel.setText("Представления");
		    slateListCaptionPanel.add(slateListCaptionLabel);   
	    rootPanel.setWidget(0, 0, slateListCaptionPanel);
	    rootPanel.getCellFormatter().setWidth(0, 0, SLATE_LIST_PANEL_WIDTH);
    	    //slateListPanel.addStyleName("applicationSlateList");
	        slateListPanel.setWidth("100%");
			rootPanel.setWidget(1, 0, slateListPanel);
		rootPanel.getCellFormatter().setWidth(1, 0, SLATE_LIST_PANEL_WIDTH);
		    FlexTable slateCaptionDecoratorPanel = new FlexTable();
		    slateCaptionDecoratorPanel.setWidth("100%");
		        SimplePanel slateCaptionPanel = new SimplePanel(); 
 			    slateCaptionPanel.addStyleName("applicationSlateCaption");
 			        slateCaptionPanel.add(slateCaptionLabel);
	        slateCaptionDecoratorPanel.setWidget(0, 0, slateCaptionPanel);
	            slateCloseButton.setWidth("100%");
	        slateCaptionDecoratorPanel.setWidget(0, 1, slateCloseButton);
	        slateCaptionDecoratorPanel.getCellFormatter().setWidth(0, 1, "30px");
		rootPanel.setWidget(0, 1, slateCaptionDecoratorPanel);
		rootPanel.getCellFormatter().addStyleName(1, 0, "applicationContentContainer");
        	//slateDecoratorPanel.setWidth("100%");
    	    slateContainerPanel.addStyleName("applicationSlateContainer");  
	        slateContainerPanel.setWidth("100%");  
		rootPanel.setWidget(1, 1, slateContainerPanel);
		rootPanel.getCellFormatter().addStyleName(1, 1, "applicationContentContainer");
	}
	
	class ApplicationAgentImpl implements ApplicationAgent
	{

		@Override
		public RedBookServiceAsync getRpcService()
		{
			return rpcService;
		}

		@Override
		public void reactivateSlate(
				Slate _Slate)
		{
			activateSlate(_Slate);
		}

		@Override
		public void openSlate(
				Slate _Slate)
		{
			ApplicationContainer.this.openSlate(_Slate);
		}
	}

	public void setEventBus(
			HandlerManager _EventBus)
	{
		eventBus = _EventBus;
	}

	public void setRpcService(
			RedBookServiceAsync _RpcService)
	{
		rpcService = _RpcService;
	}

	public void openSlate(
			Slate _NewSlate)
	{
		_NewSlate.preInitialize(applicationAgent);
		_NewSlate.initialization();
		slateList.add(_NewSlate);
		slateListPanel.reflectSlateAppending(_NewSlate);
		/**/
		activateSlate(_NewSlate);
	}

	private void activateSlate(
			Slate _Slate)
	{
		slateContainerPanel.clear();
		slateContainerPanel.add(_Slate.getTopPlate());
		slateCaptionLabel.setText(_Slate.getTitle());
		/**/
		slateListPanel.reflectSlateActivation(_Slate);
		/**/
		activeSlate = _Slate;
	}
	
	private void performSlateClose()
	{
		if (activeSlate != null)
		{
			if (activeSlate.getPlateCount() > 1)
			{
				activeSlate.closeTopPlate();
			}
			else
			{
				//activeSlate.closeTopPlate();
				slateList.remove(activeSlate);
				slateListPanel.reflectSlateClosing(activeSlate);
				if (slateList.isEmpty())
				{
					activeSlate = null;
					slateContainerPanel.clear();
				}
				else
				{
					activeSlate = slateList.get(slateList.size() - 1);
					activateSlate(activeSlate);
				}
			}
		}
	}

}
